package com.work.service.main.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.work.constant.ExceptionDescription;
import com.work.dto.main.TallyPassSubmitDTO;
import com.work.entity.main.TallyPass;
import com.work.mapper.main.TallyPassMapper;
import com.work.service.main.ITallyPassService;
import org.springblade.core.mp.base.BaseServiceImpl;
import org.springblade.core.tool.utils.Func;
import org.springframework.stereotype.Service;

/**
 * 理货达标接口实现
 *
 * @author ：dujianyu
 * @since ：Created in 2024/3/21 9:03
 */
@Service
public class TallyPassServiceImpl extends BaseServiceImpl<TallyPassMapper, TallyPass> implements ITallyPassService {
    @Override
    public boolean submitTallyPass(TallyPassSubmitDTO dto) {
        QueryWrapper<TallyPass> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .eq(TallyPass::getIsDeleted, 0)
                .eq(!Func.isNull(dto.getId()), TallyPass::getId, dto.getId())
                .eq(TallyPass::getTallyId, dto.getTallyId())
                .last("limit 1");
        TallyPass tallyPass = baseMapper.selectOne(queryWrapper);
        if (Func.isNull(tallyPass)) {
            tallyPass = new TallyPass();
        }
        tallyPass.setTallyId(dto.getTallyId());
        tallyPass.setTShipBillId(dto.getTShipBillId());
        tallyPass.setTransportBillId(dto.getTransportBillId());
        tallyPass.setTransportShipId(dto.getTransportShipId());
        tallyPass.setKeeper(dto.getKeeper());
        tallyPass.setKeeperName(dto.getKeeperName());
        tallyPass.setIsBedding(dto.getIsBedding());
        tallyPass.setIsCover(dto.getIsCover());
        tallyPass.setIsSafeProduction(dto.getIsSafeProduction());
        return super.saveOrUpdate(tallyPass);
    }

    @Override
    public boolean removeByTallyId(Long tallyId) {
        QueryWrapper<TallyPass> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .select(TallyPass::getId)
                .eq(TallyPass::getIsDeleted, 0)
                .eq(TallyPass::getTallyId, tallyId)
                .last("limit 1");
        TallyPass tallyPass = baseMapper.selectOne(queryWrapper);
        if (Func.isNull(tallyPass)) {
            throw new RuntimeException(ExceptionDescription.DB_ERROR + "没有对应的理货达标数据");
        }
        return super.deleteLogic(Func.toLongList(tallyPass.getId().toString()));
    }
}
